// pages/index/index.js
import request from '../../utils/request'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerImages: [],
    recommendList: [],
    topList: []
  },

  toRecommendSong() {
    wx.navigateTo({
      url: '/pages/songRecommend/songRecommend'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let bannerListData = await request('/banner', { type: 2 });
    this.setData({
      bannerImages: bannerListData.banners
    });

    let recommendListData = await request('/personalized', { limit: 10 });
    this.setData({
      recommendList: recommendListData.result
    });

    let index = 0;
    let tmpList = [];
    let topListData = await request('/toplist');
    while(index++ < 5){
      let playListId = topListData.list[index].id;
      let playListData = await request('/playlist/detail', {id: playListId});
      let tempDict = {name: topListData.list[index].name, tracks: playListData.playlist.tracks.slice(0, 3)};

      tmpList.push(tempDict);
      this.setData({
        topList: tmpList
      });
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})